﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace CSConsoleTest
{
    class TestExpression : BaseConsoleTest
    {
        public void printDescription()
        {
            Console.WriteLine("--- Expression ---");
        }

        public void doTest()
        {
            //Expression : 6 * 2 + (x - y)
            Expression const1 = Expression.Constant(6);
            Expression const2 = Expression.Constant(2);
            Expression leftExp = Expression.Multiply(const1, const2);
            Expression param1 = Expression.Parameter(typeof(int));
            Expression param2 = Expression.Parameter(typeof(int));
            Expression rightExp = Expression.Subtract(param1, param2);
            Expression resultExp = Expression.Add(leftExp, rightExp);
            Expression<Func<int, int, int>> expression1
                = Expression<Func<int, int, int>>.Lambda<Func<int, int, int>>(
                    resultExp,
                    new ParameterExpression[] { (ParameterExpression)param1, (ParameterExpression)param2 }
                );
            Func<int, int, int> func1 = expression1.Compile();
            Console.WriteLine("Expression1 : 6 * 2 + ({0} - {1}) = {2}", 5, 2, func1(5, 2));

            //Immutable Expression : 6 * 2 + (x - y)
            Expression<Func<int, int, int>> expression2 = (a, b) => 6 * 2 + (a - b);
            Func<int, int, int> func2 = expression2.Compile();
            Console.WriteLine("Expression2 : 6 * 2 + ({0} - {1}) = {2}", 5, 2, func2(5, 2));
        }
    }
}
